/*
 * touch-api.c
 *
 *  Created on: 2010/05/27
 *      Author: Administrator
 */

#include "touch-api.h"
#include "../control.h"

/*using spi function*/
#include "../spi/ssp.h"
#include "../gpio/gpio.h"
/*using lcd define value */
#include "../lcd-api/lcd-api.h"


//keep command of SPI use
uint8_t src_addr[SSP_BUFSIZE];
uint8_t dest_addr[SSP_BUFSIZE];

/*Global X,Y position */
static uint16_t Gx=0;
static uint16_t Gy=0;
/*Get new position flag */
static int8_t TouchGlobalFlag=false;

//init touch panel
void init_Touch(void){

	  //LPC_IOCON->PIO0_2 &= ~0x07;	/* SSP SSEL is a GPIO pin */
	  //GPIOSetValue( PORT0, 2, 1 );

	  /* port0, bit 2 is set to GPIO output and high */
	  GPIOSetDir( TOUCH_PORT,TOUCH_PEN_CS, OUTPUT);

	  SSPInit();			/* initialize SSP port, share pins with SPI0
							on port0(p0.15-18). */
	  GPIOSetValue(TOUCH_PORT,TOUCH_PEN_CS,PIN_OFF);// touch panel chip select always set

	  /* use port0_3 as input event, interrupt test. */
	  GPIOSetValue(TOUCH_PORT,TOUCH_PEN_INTERRUPT_PIN,PIN_ON);//set pin to 1 first
	  GPIOSetDir(TOUCH_PORT,TOUCH_PEN_INTERRUPT_PIN, INPUT);

	  /* port0_3, level , active low. */
	  GPIOSetInterrupt( TOUCH_PORT,TOUCH_PEN_INTERRUPT_PIN, 1, 0, 0);
	  GPIOIntEnable( TOUCH_PORT, TOUCH_PEN_INTERRUPT_PIN);

}

/*Interrupt handle function for PIO2*/
void PIOINT0_IRQHandler(void)
{
  uint32_t regVal=0;
  static uint16_t px_old=0,py_old=0;
  static uint32_t counter=0;

  regVal = GPIOIntStatus(TOUCH_PORT, TOUCH_PEN_INTERRUPT_PIN  );
  if ( regVal ) // this is interrupt from touch pen
  {

	  		//clear interrupt
	  		GPIOIntClear(TOUCH_PORT,TOUCH_PEN_INTERRUPT_PIN);
	  		//disable touch pen interrupt and enable it after draw pointer successed
	  	    GPIOIntDisable( TOUCH_PORT, TOUCH_PEN_INTERRUPT_PIN);

	  	    //delay for sometime then read value
	  	    Delayms(5);

	  		while(1){
					uint16_t px=0,py=0;
					//GPIOSetValue( PORT0, 2, 0 );// touch panel chip select set
					/* Test SPI */
					//src_addr[0] = 0xDB;			/* get x position : |S(1)|X postion(101)|eightbit(1)|def(0)|alwayson(11)*/
					//src_addr[0] = 0xD3;			/* get x position : |S(1)|X postion(101)|twelve bit(0)|def(0)|alwayson(11)*/
					src_addr[0] = 0x94;			/* get x position : |S(1)|X postion(001)|twelve bit(0)|ref(1)|powerdown(00)*/

					SSPSend( (uint8_t *)src_addr, 1 );
					//dest_addr[0]=0;
					//dest_addr[1]=0;
					SSPReceive( (uint8_t *)dest_addr, 2 );  /* get x */

					//px=MAX_WIDTH-((dest_addr[1]<<8)+dest_addr[0]);
					//px=MAX_WIDTH-(((dest_addr[0]<<4)+(dest_addr[1]>>4))-TOUCH_RIGHT_X)* (MAX_WIDTH /(TOUCH_LEFT_X-TOUCH_RIGHT_X));
					py=((dest_addr[0]<<5) | (dest_addr[1]>>3));
					/* Test SPI */
					//src_addr[0] = 0x9B;			/* get y position : |S(1)|Y postion(001)|eightbit(1)|def(0)|alwayson(11)*/
					//src_addr[0] = 0x93;			/* get y position : |S(1)|Y postion(001)|twelve bit(0)|def(0)|alwayson(11)*/
					src_addr[0] = 0xD4;			/* get y position : |S(1)|Y postion(101)|twelve bit(0)|def(1)|powerdown(00)*/

					SSPSend( (uint8_t *)src_addr, 1 );
					//dest_addr[0]=0;
					//dest_addr[1]=0;
					SSPReceive( (uint8_t *)dest_addr, 2 );  /* get y */
					//GPIOSetValue( PORT0, 2, 1 );

					//py=MAX_HEIGHT-((dest_addr[1]<<8)+dest_addr[0]);
					//py=MAX_HEIGHT-(((dest_addr[0]<<4)+(dest_addr[1]>>4))-TOUCH_BOTTOM_Y)* (MAX_HEIGHT /(TOUCH_TOP_Y-TOUCH_BOTTOM_Y));
					px=(((dest_addr[0]<<5)|(dest_addr[1]>>3)));
#if 0
					src_addr[0] = 0xE7;			/* get aux position : |S(1)|aux (110)|twelve bit(0)|ref(1)|alwayson(11)*/

					SSPSend( (uint8_t *)src_addr, 1 );
					dest_addr[0]=0;
					dest_addr[1]=0;
					SSPReceive( (uint8_t *)dest_addr, 2 );  /* get aux */
#endif
					//GPIOSetValue( PORT0, 2, 1 );// touch panel chip select clear

					//if((px==px_old)&&(py==py_old)){
						//if(counter>=MAX_TOUCH_COUNT){

							//disable touch pen interrupt and enable it after draw pointer successed
							//GPIOIntDisable( TOUCH_PORT, TOUCH_PEN_INTERRUPT_PIN);
							//clear interrupt
							//GPIOIntClear(TOUCH_PORT,TOUCH_PEN_INTERRUPT_PIN);
						    //draw_Position(px,py);
					        if(px<TOUCH_RIGHT_X) px=TOUCH_RIGHT_X;
					        if(py<TOUCH_BOTTOM_Y) py=TOUCH_BOTTOM_Y;

							Gx=MAX_WIDTH-((px-TOUCH_RIGHT_X)/SCALE_X);
							Gy=MAX_HEIGHT-((py-TOUCH_BOTTOM_Y)/SCALE_Y);

							if(Gx>MAX_WIDTH)Gx=Gx-MAX_WIDTH;
							if(Gy>MAX_HEIGHT) Gy=Gy-MAX_HEIGHT;

							TouchGlobalFlag=true; //got new position
							//counter=0;//clear counter
							break; //break while

						//}else{
							//counter++;
						//}
					//}else{
						//px_old=px;
						//py_old=py;
						//if(counter>0)
						 //counter--;//clear counter
						//counter=0;
					//}

	  		}//end while 1
#if 0
			FillRectangle(0xFFFF,
	    		       		  GX,GY,
	    		       		  5,5);
#endif

}//end int from PIO0_2

  return;
}

/*get TouchGlobalFlag value */
uint8_t get_TouchGlobalFlag(){
	return TouchGlobalFlag;
}

/*set TouchGlobalFlag value */
void set_TouchGlobalFlag(int8_t value){
	TouchGlobalFlag=value;
	if(value==false)//enable touch pen interrupt
	  GPIOIntEnable( TOUCH_PORT, TOUCH_PEN_INTERRUPT_PIN);
}

/*get Gx value*/
uint16_t get_Gx()
{
  return Gx;
}

/*get Gy value*/
uint16_t get_Gy()
{
  return Gy;
}
